/***     
 * 找三张
 */

var _ = require('./../../FrameWork/we_underscore');
var FindSameCardValueTool = require('./FindSameCardValueTool');
var FindShunZiTool = require('./FindShunZiTool');
var FindPairsTool = require('./FindPairsTool');

var FindThree = module.exports = {};

/***就只获取三张牌 */
FindThree.getThreeResult = function(_cardInfoList) {
    var size = _.size(_cardInfoList);
    if (size < 3) {
        return {
            isFind: false,
            findGroup: [],
            otherGroup: _cardInfoList
        }
    }
    var tmpCards = FindSameCardValueTool.check(_cardInfoList);

    var threeCards = _.filter(tmpCards, function(_cards) {
        return _.size(_cards) == 3;
    });

    var findGroupMore = [];

    _.each(threeCards, function(cards) {
        findGroupMore.push({
            findGroup: cards,
            otherGroup: _.difference(_cardInfoList, cards)
        })
    });

    var result = {
        isFind: _.size(findGroupMore) > 0,
        findGroupMore: findGroupMore,
        otherGroup: _.size(findGroupMore) > 0 ? [] : _cardInfoList,
    }
    return result;
};


/***是否为三带一*/
FindThree.isBeltOne = function(_cardInfoList) {
    var size = _.size(_cardInfoList);
    if (4 != size) {

    }
    var tmpCards = FindSameCardValueTool.check(_cardInfoList);
    if (2 != _.size(tmpCards)) {
        return false;
    }

    var findCards = _.filter(tmpCards, function(_cards) {
        return 3 == _.size(_cards);
    });
    return _.size(findCards) == 1;
};

/**三带1*/
FindThree.getBeltOneMore = function(_cardInfoList) {
    var resultInfo = FindSameCardValueTool.getResult(_cardInfoList, 3);
    var findGroupMore = [];
    if (resultInfo.isFind) {
        _.each(resultInfo.findGroup, function(singleGroup) {
            var findGroup = singleGroup;
            var otherGroup = _.difference(_cardInfoList, findGroup);
            findGroup.push(_.last(otherGroup));
            otherGroup = _.initial(otherGroup);
            findGroupMore.push({
                findGroup: findGroup,
                otherGroup: otherGroup
            });
        });
    }
    var result = {
        isFind: _.size(findGroupMore) > 0,
        findGroupMore: findGroupMore,
        otherGroup: _.size(findGroupMore) > 0 ? [] : _cardInfoList,
    }
    return result;
};

/***是否为三张一样的牌*/
FindThree.isFind = function(_cardInfoList) {
    var size = _.size(_cardInfoList);
    if (3 != size) {
        return false;
    }
    var isFind = _cardInfoList[0].value == _cardInfoList[1].value &&
        _cardInfoList[0].value == _cardInfoList[2].value;
    return isFind;
};


/**三带二*/
FindThree.getBeltTwoMore = function(_cardInfoList) {

};

/***是否为三带二*/
FindThree.isBeltTwo = function(_cardInfoList) {
    var size = _.size(_cardInfoList);
    if (5 != size) {
        return false;
    }
    var tmpCards = FindSameCardValueTool.check(_cardInfoList);
    if (3 != _.size(tmpCards)) {
        return false;
    }
    return true;
};

/**三带对*/
FindThree.getBeltPairsMore = function(_cardInfoList) {
    var resultInfo = FindSameCardValueTool.getResult(_cardInfoList, 3);
    var findGroupMore = [];
    if (resultInfo.isFind) {
        _.each(resultInfo.findGroup, function(singleGroup) {
            var findGroup = singleGroup;
            var otherGroup = _.difference(_cardInfoList, findGroup);
            var tmpResult = FindPairsTool.getResult(otherGroup);
            if (tmpResult.isFind) {
                findGroup.push(tmpResult.findGroup[0]);
                findGroup = _.flatten(findGroup);
                otherGroup = _.difference(_cardInfoList, findGroup);
                findGroupMore.push({
                    findGroup: findGroup,
                    otherGroup: otherGroup
                })
            }
        });
    }
    var result = {
        isFind: _.size(findGroupMore) > 0,
        findGroupMore: findGroupMore,
        otherGroup: _.size(findGroupMore) > 0 ? [] : _cardInfoList,
    }
    return result;
};

/***是否为三带对*/
FindThree.isBeltPairs = function(_cardInfoList) {
    var size = _.size(_cardInfoList);
    if (5 != size) {
        return false;
    }
    var tmpCards = FindSameCardValueTool.check(_cardInfoList);
    if (2 != _.size(tmpCards)) {
        return false;
    }
    return true;
};

/***飞机*/
FindThree.getAircraftMore = function(_cardInfoList) {
    if (true == this.isCanAircraft(_cardInfoList)) {
        var info = this.checkAircraft(_cardInfoList);
        var threeGroupList = info.threeGroupList;
        var singleList = _.map(threeGroupList, function(_list) {
            return _list[0];
        });
        var findGroupMore = [];
        var result = FindShunZiTool.getMoreResultSection(singleList, 2, _.size(singleList));
        if (result.isFind) {
            _.each(result.findGroupMore, function(_findGroupMore) {
                //就在这层组件飞机
                var _tmpFindGroup = [];
                var findGroup = _findGroupMore.findGroup;
                _.each(findGroup, function(_cardInfo) {
                    var value = _cardInfo.value;
                    for (var i = 0; i < _.size(threeGroupList); ++i) {
                        if (value == threeGroupList[i][0].value) {
                            _tmpFindGroup.push(_.first(threeGroupList[i], 3))
                        }
                    }
                });

                /***这里将三张一样的牌加入 */
                _tmpFindGroup = _.flatten(_tmpFindGroup);
                // findGroupMore.push(_tmpFindGroup);
                var _size = _.size(_tmpFindGroup);
                /***计算出飞机要带的牌数量 */
                var needBeltCardCnt = _size / 3;
                var _otherGroup = _.difference(_cardInfoList, _tmpFindGroup);
                var otherSize = _.size(_otherGroup);

                //查找是否能找到带对子
                if ((needBeltCardCnt * 2) <= otherSize) {
                    //可能带对子
                    var tmpCards = FindSameCardValueTool.check(_otherGroup);
                    tmpCards = _.filter(tmpCards, function(cards) {
                        return _.size(cards) >= 2;
                    });

                    if (_.size(tmpCards) >= needBeltCardCnt) {
                        for (var j = 0; j < needBeltCardCnt; ++j) {
                            _tmpFindGroup.push(tmpCards[j][0]);
                            _tmpFindGroup.push(tmpCards[j][1]);
                        }
                        //带对子咯
                        findGroupMore.push(_tmpFindGroup);
                        return;
                    }
                }
                //带单 
                if (needBeltCardCnt <= otherSize) {
                    _otherGroup = _.sortBy(_otherGroup, function(_cardInfo) {
                        return _cardInfo.value;
                    });
                    for (var j = 0; j < needBeltCardCnt; ++j) {
                        _tmpFindGroup.push(_otherGroup[j]);
                    }
                    //带单咯
                    findGroupMore.push(_tmpFindGroup);
                    return;
                }
                findGroupMore.push(_tmpFindGroup);
            });
            findGroupMore = _.flatten(findGroupMore);
            return {
                isFind: true,
                findGroup: findGroupMore,
                otherGroup: _.difference(_cardInfoList, findGroupMore)
            };
        }
    }
    return {
        isFind: false,
        findGroup: [],
        otherGroup: _cardInfoList
    };
};


/***是否为飞机
 * 智能带单，或者对，或者不带
 */
FindThree.isAircraft = function(_cardInfoList) {
    var info = this.checkAircraft(_cardInfoList);
    var threeGroupList = info.threeGroupList;
    var otherGroupList = info.otherGroupList;
    if (_.size(threeGroupList) >= 2) {
        var tmpFristCards = _.map(threeGroupList, function(_cards) {
            return _cards[0];
        })
        if (true == FindShunZiTool.isShunZi(tmpFristCards, _.size(threeGroupList))) {
            var otherCardsSize = _.size(otherGroupList);
            var threeGroupListSize = _.size(threeGroupList);
            if (0 == otherCardsSize || threeGroupListSize == otherCardsSize || threeGroupListSize * 2 == otherCardsSize) {
                return true;
            }
        }
    }
    return false;
};


/***是否可能飞机*/
FindThree.isCanAircraft = function(_cardInfoList) {
    var info = this.checkAircraft(_cardInfoList);
    var threeGroupList = info.threeGroupList;
    /***可能是飞机 */
    if (_.size(threeGroupList) >= 2) {
        var singleList = _.map(threeGroupList, function(_list) {
            return _list[0];
        })
        return 0 != FindShunZiTool.isShunZiSection(singleList, 2, _.size(singleList));
    }
    return false;
};

/***分析飞机将三张或者4张一样的牌跟单牌对子隔开 */
FindThree.checkAircraft = function(_cardInfoList) {
    var tmpCards = FindSameCardValueTool.check(_cardInfoList);
    /***3张或者4张牌牌合集 */
    var threeGroupList = _.filter(tmpCards, function(_singleCards) {
        return _.size(_singleCards) >= 3;
    });
    threeGroupList = _.compact(threeGroupList);
    /***其他牌 */
    var otherGroupList = _.flatten(_.filter(tmpCards, function(_singleCards) {
        return _.size(_singleCards) < 3;
    }));
    return {
        threeGroupList: threeGroupList,
        otherGroupList: otherGroupList
    };
};